import {Component} from 'react';
import {check} from '../cas';
import CasContext from '../CasContext';

export default function(data) {
  return function(Ele){
    return class CasAuthorized extends Component{
      static contextType = CasContext;
      constructor(props){
        super(props);
        this.state = {
          loading:true,
          ticket:null,
          user:null
        }
      }
      async componentDidMount(){
        const {ticket} = this.context;
        console.log(this.context);
        if(ticket !== ''){
          this.setState({
            ...this.context,
            loading:false
          })
        }else{
          const re = await check();
          this.setState({
            ...re,
            loading:false
          })
        }
      }
      render(){
        const {
          loading
        } = this.state;
        if(loading){
          return data.loading;
        }
        return (
          <CasContext.Provider value={{
            ...this.state
          }}>
            <Ele {...this.props} {...this.state}  />
          </CasContext.Provider>
        );
      }
    }
  }
}